<template>
  <div class="music-platform">
    <main>
      <!-- 轮播图 -->
      <section class="banner">
        <div class="banner-content">
          <el-carousel :interval="5000" type="card" height="500px">
            <el-carousel-item v-for="(image, index) in images" :key="index">
              <img :src="image" alt="Carousel Image" style="width: 100%; height: 100%; object-fit: cover;" />
            </el-carousel-item>
          </el-carousel>
        </div>
      </section>

      <!-- 乐器板块 -->
      <section class="music-instrument">
        <div class="instrument-header">
          <h2>乐器</h2>
          <el-button type="text" class="more-notations" @click="goToNotation('/instrument')">
            <h4>>> 更多</h4>
          </el-button>
        </div>
        <div class="card-container" style="margin-top: 20px;">
          <div class="card" v-for="(instrument, index) in instruments" :key="instrument.instrument_id">
            <div class="card-image" @click="goToInstrumentDetail(instrument.instrument_id)">
              <img :src="'http://localhost:8080/images/upload/' + instrument.image" alt="Instrument image" class="instrument-image" />
            </div>
            <div class="card-content">
              <h3>{{ instrument.name }}</h3>
            </div>
          </div>
        </div>
      </section>

      <!-- 音乐文化背景板块 -->
      <section class="music-culture">
        <div class="instrument-header">
          <h2>文化背景</h2>
          <el-button type="text" class="more-notations" @click="goToNotation('/Culture/back')">
            <h4>>> 更多</h4>
          </el-button>
        </div>
        <div class="card-container" style="margin-top: 20px;">
          <div class="card" v-for="(detail, index) in details" :key="detail.culture_background_detail_id">
            <div class="card-image" @click="goToCultureDetail(detail.culture_background_detail_id)">
              <img :src="'http://localhost:8080/images/upload/' + detail.image" alt="Culture background image" class="instrument-image" />
            </div>
            <div class="card-content">
              <h3>{{ detail.title }}</h3>
            </div>
          </div>
        </div>
      </section>
    </main>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 轮播图和乐器、文化详情的图片
      images: [
        'http://localhost:8080/images/upload/乐器1.jpg',
        'http://localhost:8080/images/upload/乐器2.jpg',
        'http://localhost:8080/images/upload/乐器3.jpg',
        'http://localhost:8080/images/upload/乐器4.jpg',
      ],
      instruments: [],
      details: [],
    };
  },

  methods: {
    fetchHotInstruments() {
      fetch('http://localhost:8080/api/instruments/hot')
        .then(response => response.json())
        .then(data => {
          this.instruments = data;
        })
        .catch(error => console.error('Error fetching instruments:', error));
    },
    fetchHotBackground() {
      fetch('http://localhost:8080/api/culture-background-detail/hot')
        .then(response => response.json())
        .then(data => {
          this.details = data;
        })
        .catch(error => console.error('Error fetching details:', error));
    },
    goToNotation(path) {
      this.$router.push(path);
    },
    goToInstrumentDetail(instrumentId) {
      this.$router.push(`/instrument/${instrumentId}`);
    },
    goToCultureDetail(detailId) {
      this.$router.push(`/Culture/back/${detailId}`);
    }
  },

  created() {
    this.fetchHotInstruments();
    this.fetchHotBackground();
  },
};
</script>

<style scoped>
body {
  font-family: 'KaiTi', KaiTi, '楷体', serif;
  background-color: #f5f5f5;
  color: #333;
  margin: 0;
  padding: 0;
}

main {
  padding: 20px;
  margin-left: 60px;
  margin-right: 60px;
}

/* 轮播图样式 */
.banner {
  background-color: #ffffff;
  padding: 15px;
  border-radius: 8px;
  margin-bottom: 30px; /* 增加轮播图与下方内容的间距 */
}

.banner-content {
  position: relative;
}

/* 乐器和文化背景板块样式 */
.music-instrument,
.music-culture {
  background-color: #ffffff;
  padding: 15px;
  border-radius: 8px;
  margin-top: 30px;
}

.instrument-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 2px solid #ddd;
  padding-bottom: 5px;
}

.instrument-header h2 {
  margin: 0;
  font-size: 1.8em;
}

.music-instrument h2,
.music-culture h2 {
  font-size: 1.8em;
  border-bottom: 2px solid #d7bc34;
  padding-bottom: 10px;
}

.card-container {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  justify-content: space-between;
}

.card {
  background-color: #f9f9f9;
  padding: 0;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  flex: 1;
  min-width: 160px;
  max-width: 220px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.card-image {
  width: 100%;
  height: 180px;
  overflow: hidden;
  flex-shrink: 0;
}

.instrument-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.card-content {
  padding: 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
}

.card h3 {
  font-size: 1.2em;
  color: #384538;
}

.card p {
  font-size: 1em;
  color: #666;
}

/* “查看更多”按钮样式 */
.more-notations h4 {
  margin: 0;
  font-size: 1.2em;
  color: black;
  font-weight: bold;
}

router-link {
  text-decoration: none !important;
}
</style>
